<template>
  <view class="send-express">
    <u-sticky bgColor="#fff">
      <u-tabs :list="expressTab" :current="current" :scrollable="false" @click="tabClick"></u-tabs>
    </u-sticky>
    <view class="send-express-card">
      <view class="send-express-card-send">
        <view class="send-info">
          <image class="send-icon" src="@/static/sendExpress/send.png" mode="widthFix" />
          <view class="send-info-content" @click="goAddressBook('send')">
            <view class="send-info-title">{{sendInfoContent.sendName}}</view>
            <view class="send-info-tips">
              <text class="tips-text">{{ sendInfoContent.sendAddress }}</text>
              <u-icon name="arrow-right" color="#999"></u-icon>
            </view>
          </view>
        </view>
        <view class="address-book" @click="goAddressBook('send')">地址簿</view>
      </view>
      <view class="send-express-card-round--trip" v-if="selectTab !== 'batch'">
        <image src="@/static/sendExpress/round_trip.png" class="round--trip-icon" mode="scaleToFill" />
        <view class="round--trip-border"></view>
      </view>
      <view class="send-express-card-receive" v-if="selectTab !== 'batch'">
        <view class="receive-info" @click="goAddressBook('receive')">
          <image class="receive-icon" src="@/static/sendExpress/receive.png" mode="widthFix" />
          <view class="receive-info-content">
            <view class="receive-info-title">{{ receiveInfoContent.receiveName }}</view>
            <view class="receive-info-tips">
              <text class="tips-text">{{ receiveInfoContent.receiveAddress }}</text>
              <u-icon name="arrow-right" color="#999"></u-icon>
            </view>
          </view>
        </view>
        <view class="address-book" @click="goAddressBook('receive')">地址簿</view>
      </view>
    </view>
      <!-- 批量寄件列表 -->
    <view class="receive-list" v-if="selectTab === 'batch' && receiveList.length">
      <view class="receive-list-card" v-for="(item,index) in receiveList" :key="index">
        <view class="send-express-card-receive">
          <view class="receive-info">
            <image class="receive-icon" src="@/static/sendExpress/receive.png" mode="widthFix" />
            <view class="receive-info-content">
              <view class="receive-info-title">{{ item.receiveName }}</view>
              <view class="receive-info-tips">
                <text class="tips-text">{{ item.receiveAddress }}</text>
                <u-icon name="arrow-right" color="#999"></u-icon>
              </view>
            </view>
          </view>
          <view class="address-book" @click="delrRceive(index)">
            <u-icon name="trash" color="#999"></u-icon>
          </view>
        </view>
      </view>
    </view>
    <!-- 批量寄件添加 -->
    <view class="add-receive-arr" v-if="selectTab === 'batch'">
      <view class="plus-receive-btn" @click="addReceive()">
        <u-icon name="plus" size="14"></u-icon>
        <text>添加收件人</text>
      </view>
    </view>
  
    <!-- 选择物流信息 -->
    <view class="send-express-prod">
      <view class="prod-title">物品信息</view>
      <view class="prod-tips" @click="showPordPopup">
        <text class="tips-text text-bold" v-if="prodInfoText">{{ prodInfoText }}</text>
        <text class="tips-text" v-else>请选择物品信息</text>
        <u-icon name="arrow-right" color="#666"></u-icon>
      </view>
    </view>
    <view class="send-express-pick-up">
      <view class="pick-up-title">快递公司</view>
      <view class="pick-up-card">京东快递</view>
      <view class="pick-up-time"  @click="selectPickUp">
        <view class="pick-up-time-title">期望上门时间</view>
        <view class="pick-up-time-select">
          <text class="select-show-time">{{ checkDateTime }}</text>
          <u-icon name="arrow-right" color="#999"></u-icon>
        </view>
      </view>
    </view>
    <view class="send-express-pay">
      <view class="send-express-pay-tips" v-if="expressTips">{{ expressTips }}</view>
      <view class="send-express-pay-btns">
        <view class="pay-detail">
          <view class="pay-detail-text">
            <text>预计：</text>
            <text>{{totalPrice}}元</text>
          </view>
          <!-- <view></view> -->
        </view>
        <view class="pay-btns">
          <!-- 微信账号展示 -->
          <view class="pay-btns-one" v-if="userType === 'wechat'">
            <text>0元下单</text>
            <text>(先寄后付)</text>
          </view>
          <u-button class="pay-btns-two" :loading="payLoading" v-if="userType === 'wechat'" type="primary" size="small" text="付款" @click="wechatMakeOrde"></u-button>
          <!-- 商户账号展示 -->
          <u-button class="pay-btns-two" :loading="orderLoading"  v-if="userType === 'system'" type="primary" size="small" text="下单" @click="merchantMakeOrder"></u-button>
        </view>
      </view>
    </view>

    <!-- 物品选择 -->
    <u-popup :show="prodShow" @close="close" :closeable="true">
      <view class="prod-info-box">
        <view class="prod-info-box--title">
          <view>物品信息</view>
          <view class="prod-banned" @click="hanlderBannedProd">
            <text>违禁物品详情</text>
            <u-icon name="arrow-right" color="#999"></u-icon>
          </view>
        </view>
        <view class="prod-info-box-main">
        <!-- 商户账号展示 -->
          <view class="main-card card-weight"  v-if="userType === 'system'" @click="selectProd">
            <view class="main-card-title">
              <text>选择产品</text>
              <text>必填</text>
            </view>
            <view class="main-card-input">
              <text>{{ prodinfo }}</text>
              <u-icon name="arrow-right" color="#999"></u-icon>
            </view>
          </view>
          <!-- 微信账号展示 -->
          <view class="main-card"  v-if="userType === 'wechat'">
            <view class="main-card-title">
              <text>物品名称</text>
              <text>必填</text>
            </view>
            <view class="main-card-input">
              <u--input
                class="card-input"
                placeholder="请输入物品名称"
                border="surround"
                :clearable="true"
                v-model="prodType"
              ></u--input>
            </view>
          </view>
          <view class="main-card" >
            <view class="main-card-title">
              <text>物品类型</text>
              <text></text>
            </view>
            <view class="main-card-input prod-card-box">
              <view class="prod-card" :class="{ 'prod-card-active': item.id === selcetProdType }" v-for="(item,index) in columns" :key="index" @click="handSelectProdType(item)">{{ item.label }}</view>
            </view>
          </view>
          <view class="main-card card-weight">
            <view class="main-card-title">
              <text>重量(KG)</text>
              <text>必填</text>
            </view>
            <view class="main-card-input">
              <u-number-box v-model="weight" inputWidth="50" :step="0.1" :min="selectTab !== 'big' ? 0 : 20" :decimalLength="2" :max="selectTab === 'send'? 20 : 2000" @overlimit="overlimitWeight"></u-number-box>
            </view>
          </view>
          <view class="main-card card-weight">
            <view class="main-card-title">
              <view class="card-title-volume">体积</view>
            </view>
            <view class="main-card-input volume-input">
              <text>长(CM)</text>
              <u--input
                border="bottom"
                type="number"
                clearable
                v-model="volume.long"
              ></u--input>
              <text>宽(CM)</text>
              <u--input
                border="bottom"
                type="number"
                clearable
                v-model="volume.width"
              ></u--input>
              <text>高(CM)</text>
              <u--input
                border="bottom"
                type="number"
                clearable
                v-model="volume.height"
              ></u--input>
            </view>
          </view>
          <view class="main-card">
            <view class="main-card-title">
              <!-- <view>备注</view> -->
            </view>
            <view class="main-card-input-text">
              注：包裹重量小体积大时按照体积计算重量，实际重量以订单签收后实时回传重量为准哦~， <text @click="openRule">点击了解计费规则和注意事项</text>
            </view>
          </view>
          <view class="main-card">
            <view class="main-card-title">
              <view>备注</view>
            </view>
            <view class="main-card-input">
              <u--textarea confirmType="send" v-model="remark" placeholder="请输入内容" ></u--textarea>
            </view>
          </view>
        </view>
        <view class="prod-info-box-btn">
          <button class="info-confirm" @click="confirm">确认</button>
        </view>
      </view>
    </u-popup>
    <!-- 期望上门时间 -->
    <u-popup :show="arrivalTimeShow" @close="arrivalClose" :closeable="true">
      <view class="arrival-time-box">
        <view class="arrival-time-box--title">上门时间</view>
        <view class="arrival-date-time">
          <view class="arrival-btn" :class="arrivalDateData === item.date ? 'arrival-btn-active' : ''" v-for="item in arrivalDateList" :key="item.date" @click="selectArrivalDate(item)">
            <text>{{ item.display }}</text>
            <text>{{ item.date }}</text>
          </view>       
        </view>
        <view class="arrival-date-time">
          <view class="arrival-btn" :class="arrivalTimeData === item ? 'arrival-btn-active' : ''" v-for="item in arrivalTimeList" :key="item" @click="selectArrivalTime(item)">{{ item }}</view>
        </view>
        <view class="arrival-time-box-btn">
          <button class="time-confirm" @click="arrivalConfirm">确认</button>
        </view>
      </view>
    </u-popup>
   
  </view>
</template>

<script>
import { getLocal,setLocal } from '@/utils/uni'
export default {
  data() {
    return {
      // 定义一个表达式数组，用于管理不同的快递服务类型
      expressTab: [
        {
          name: "寄快递",
          type: 'send'
        },
        {
          name: "批量寄件",
          type: 'batch'
        },
        {
          name: "寄大件",
          type: 'big'
        },
      ],
      // 定义一个用于切换选项卡的字符串常量，标识为"send"
      selectTab: 'send',
      // 初始化当前选项卡索引为0，用于显示默认选项卡
      current: 0,
      // 控制产品展示状态的布尔值，初始为false，表示产品信息未显示
      prodShow:false,
      // 用于存储当前选择的产品类型，初始为空字符串
      prodType: '',
      // 用于展示产品信息的文本，初始为空字符串
      prodInfoText: '',
      // 存储产品重量的变量，初始为空字符串
      weight: '',
      // 存储用户备注信息的变量，初始为空字符串
      remark: '',
      // 存储当前用户地址的变量，初始为空字符串
      currentAddress: '',
      // 存储当前收货地址的变量，初始为空字符串
      currentReceiveAddress: '',
      // 寄件人信息内容配置
      sendInfoContent: {
        // 寄件人名称
        sendName: '寄件人信息',
        // 寄件人地址填写说明
        sendAddress: '复制完整信息，自动智能填写'
      },
      // 收件人信息内容配置
      receiveInfoContent: {
        // 收件人名称
        receiveName: '收件人信息',
        // 收件人信息填写说明
        receiveAddress: '复制完整信息，自动智能填写'
      },
      // 表格列配置，目前未使用
      columns:[
        {
          label: '文件',
          id: 'bm000001'
        }, 
        {
          label: '数码产品',
          id: 'bm000002'
        },
        {
          label: '生活用品',
          id: 'bm000003'
        }, 
        {
          label: '食品',
          id: 'bm000004'
        },
        {
          label: '服饰',
          id: 'bm000005'
        }, 
        {
          label: '其他',
          id: 'bm000006'
        },
        {
          label: '生鲜类',
          id: 'bm000007'
        }, 
        {
          label: '易碎品',
          id: 'bm000008'
        },
        {
          label: '液体',
          id: 'bm000009'
        }
      ],
      // 产品信息，目前为空
      prodinfo: '',
      // 定义体积对象，用于存储长、宽、高的尺寸信息
      volume: {
        long: '', // 长度
        width: '', // 宽度
        height: '' // 高度
      },
      // 用户类型，此处设置为‘bus’表示公交用户
      userType: 'system',
      // 控制到达时间的显示状态，初始设置为不显示
      arrivalTimeShow: false,
      arrivalDateList: [],// 上门日期
      arrivalTimeList: [
        '09:00-10:00',
        '10:00-11:00',
        '11:00-12:00',
        '12:00-13:00',
        '13:00-14:00',
        '14:00-15:00',
        '15:00-16:00',
        '17:00-18:00',
        '19:00-20:00',
        '20:00-20:30'
      ], // 上门时间
      arrivalDateData: '', // 上门日期选择
      arrivalTimeData: '', // 上门时间选择
      checkDateTime: '',
      totalPrice: '--', // 预计快递费
      currentDate:  new Date(),
      receiveList: [],  // 批量寄件列表
      selectBatchIndex: '',
      tradeNo: '',
      expressTips:'',
      payLoading: false,
      orderLoading: false,
      selcetProdType: ''
    };
  },
  onLoad(options) {
    this.pageInit(options.type)
    this.userType = getLocal('userType')
  },
  onShow() {
    // 寄件地址
    if(uni.getStorageSync('currentAddress')){
      this.currentAddress = JSON.parse(decodeURIComponent(uni.getStorageSync('currentAddress')))
      this.sendInfoContent.sendAddress = this.currentAddress.province + this.currentAddress.city + this.currentAddress.area + this.currentAddress.address
      this.sendInfoContent.sendName = this.currentAddress.name +' ' + this.currentAddress.mobile
      console.log(this.sendInfoContent,'this.sendInfoContent');
      
    }
    // 收件地址
    if(uni.getStorageSync('currentReceiveAddress')){
      this.currentReceiveAddress = JSON.parse(decodeURIComponent(uni.getStorageSync('currentReceiveAddress')))
      this.receiveInfoContent.receiveAddress = this.currentReceiveAddress.province + this.currentReceiveAddress.city + this.currentReceiveAddress.area + this.currentReceiveAddress.address
      this.receiveInfoContent.receiveName = this.currentReceiveAddress.name +' ' + this.currentReceiveAddress.mobile
      console.log(this.currentReceiveAddress,'this.currentReceiveAddress');
      
    }
    if(uni.getStorageSync('currentAddress') && uni.getStorageSync('currentReceiveAddress') && this.selectTab !== 'batch'){
      this.getCourierFreightCharge()
    }
    this.onProdList()
  },
  methods: {
    hanlderBannedProd(){
      uni.navigateTo({
        url: '/cmhqPage/bannedProd/bannedProd'
      })
    },
    openRule(){
      uni.navigateTo({
        url: '/cmhqPage/billingRules/billingRules'
      })
    },
    handSelectProdType(item){
      this.selcetProdType = item.id
    },
    // 微信支付
    wechatMakeOrde(){
      this.merchantMakeOrder('wechat')
    },
    // 批量删除收件人
    delrRceive(index){
      this.receiveList.splice(index,1)
    },
    // 批量添加收件人
    addReceive(){
      uni.navigateTo({
        url: `/cmhqPage/addressBook/addressBook?type=batch`
      })
    },
    /**
     * 选择到达日期的函数
     * 
     * 此函数用于接收一个包含日期的对象，并将该对象的日期属性赋值给组件的arrivalDateData属性
     * 主要用于在用户选择了一个特定的日期后，更新组件的数据，以便后续操作或显示
     * 
     * @param {Object} item - 包含日期的对象，通常来源于用户在日期选择器中的选择
     */
    selectArrivalDate(item){
      this.arrivalDateData = item.date
    },
    // 选择到达时间
    selectArrivalTime(item){
      this.arrivalTimeData = item
    },
    // 生成日期列表
    generateDays(){
      this.arrivalDateList = [];
      for (let i = 0; i < 7; i++) {
        const date = new Date(this.currentDate);
        date.setDate(date.getDate() + i);
        const display = this.formatDisplay(i,date);
        const dateString = this.formatDate(date);
        this.arrivalDateList.push({
          display,
          date: dateString
        });
      }
    },
    // 格式化日期
    formatDisplay(index,date){
      if (index === 0) return '今天';
      if (index === 1) return '明天';
      if (index === 2) return '后天';
      return this.formatWeekday(new Date(date.setDate(date.getDate())));
    },
    // 格式化日期
    formatDate(date){  
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    // 格式化星期
    formatWeekday(date){
      const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
      return weekdays[date.getDay()];
    },
    // 选择上门取件
    selectPickUp(){
      this.generateDays()
      this.arrivalTimeShow = true
    },
    // 选择产品
    selectProd(){
      uni.navigateTo({
        url: '/cmhqPage/prodList/prodList'
      })
    },
    // 监听产品列表返回的数据
    onProdList(item){
      uni.$on('updateProd', (item) => {
        this.prodinfo = item.categoryName
        this.volume = {
          long: item.volume, // 长度
          width: item.width, // 宽度
          height: item.height // 高度
        }
        this.weight = item.weight
      })
      uni.$on('updateBatchAddress', (item) => {
        const batchAddress = JSON.parse(decodeURIComponent(item))
        console.log(batchAddress,1);
        const id = batchAddress.id
        const receiveName = batchAddress.name + ' ' + batchAddress.mobile
        const receiveAddress = batchAddress.province + batchAddress.city + batchAddress.area + batchAddress.address
        const prodInfoText = '请选择物品'
        if(this.receiveList.length > 0){
          const index = this.receiveList.findIndex(item => item.id === id)
          if(index === -1){
            this.receiveList.push({          
              receiveName,
              receiveAddress,
              prodInfoText,
              weight: '',
              prodinfo: '',
              ...batchAddress
            })
          }
        }else{
          this.receiveList.push({          
            receiveName,
            receiveAddress,
            id,
            prodInfoText,
            weight: '',
            prodinfo: '',
            ...batchAddress
          })
        }
        if(this.receiveList.length > 0){
          this.getBatchourierFreightCharge()
        }
      })
    },
    // 获取批量下单费用 courierOrder/getBatchourierFreightCharge
    getBatchourierFreightCharge(){
      let checkDate,checkTime
      if(this.checkDateTime){
        checkDate = this.checkDateTime.split(' ')
        checkTime = checkDate[1].split('-')
      }

      const  date1 = checkDate ? checkDate[0] + ' ' + checkTime[0] : ''
      const  date2 = checkDate ? checkDate[0] + ' ' + checkTime[1] : ''
      this.$http.post('courierOrder/getBatchourierFreightCharge', {resources : this.setReceiveList(date1,date2)}, res => {
        if(res.code === 'success'){
          this.totalPrice = res.data.totalPrice
          this.expressTips = res.data.info
        }
      })
    },
    // 确认
    confirm(){
      // 商户账号逻辑判断
      if(this.userType === 'system' && !this.prodinfo){
        this.$uti.alert('请选择产品')
        return
      }
      // 微信用户逻辑判断
      if(this.userType === 'wechat' && !this.prodType){
        this.$uti.alert('请输入物品类型')
        return
      }
      if(!this.selcetProdType){
        this.$uti.alert('请选择物品类型')
        return
      }
      if(!this.weight){
        this.$uti.alert('请输入重量')
        return
      }
      this.prodInfoText = this.prodinfo ? this.prodinfo : this.prodType + '/' + this.weight + 'kg'
      if(this.selectTab !== 'batch'){
        this.getCourierFreightCharge()
      }
      if(this.selectTab === 'batch'){
        this.getBatchourierFreightCharge()
      }
      this.prodShow = false
      // this.close()
    },
    // 确认上门时间
    arrivalConfirm(){
      if(!this.arrivalDateData){
        this.$uti.alert('请选择上门日期')
        return
      }
      if(!this.arrivalTimeData){
        this.$uti.alert('请选择上门时间')
        return
      }
      this.checkDateTime = this.arrivalDateData + ' ' + this.arrivalTimeData
      this.arrivalTimeShow = false
    },
    //  关闭产品弹窗
    close(){
      this.prodinfo = ''
      this.weight = ''
      this.volume.long = ''
      this.volume.width = ''
      this.volume.height = ''
      this.remark = ''
      this.prodShow = false
    },
    // 关闭上门时间弹窗
    arrivalClose(){
      this.arrivalDateData = ''
      this.arrivalTimeData = ''
      this.arrivalTimeShow = false
    },
    // 显示产品弹窗
    showPordPopup(){
      this.prodShow = true
    },
    // 页面初始化
    pageInit(options){
      let tabCurrent = {
        'ed': 0,
        'bulk': 1,
        'big': 2
      }
      this.current = tabCurrent[options]
      this.selectTab = this.expressTab[this.current].type
    },
    // 跳转地址通讯录
    goAddressBook(type){
      uni.navigateTo({
        url: `/cmhqPage/addressBook/addressBook?type=${type}`
      })
    },
    // 超出最大重量提示
    overlimitWeight(val){
      this.$uti.alert('超出最大重量限制20kg,建议选择寄大件更优惠')
    },
    // 商户下单
    merchantMakeOrder(type){
      if(!this.currentAddress){
        this.$uti.alert('请选择发货地址')
        return
      }
      if(!this.currentReceiveAddress && this.selectTab !== 'batch'){
        this.$uti.alert('请选择收货地址')
        return
      }
      if(!this.receiveList.length && this.selectTab === 'batch'){
        this.$uti.alert('请选择收货地址')
        return
      }
      if(!this.prodinfo && this.userType === 'system'){
        this.$uti.alert('请选择物品信息')
        return
      }
      if(!this.prodType && this.userType === 'wechat'){
        this.$uti.alert('请选择物品类型')
        return
      }
      if(!this.weight){
        this.$uti.alert('请输入重量')
        return
      }
      if(!this.checkDateTime){
        this.$uti.alert('请选择期望上门时间')
        return
      }
      const orderOrigin = {
        'big': '4',
        'send': '1'
      }
      const checkDate = this.checkDateTime.split(' ')
      const checkTime = checkDate[1].split('-')
      const  date1 = checkDate[0] + ' ' + checkTime[0]
      const  date2 = checkDate[0] + ' ' + checkTime[1]
      if(this.selectTab !== 'batch'){
        let param = {
          time: [date1,date2],
          fromName: this.currentAddress.name,
          fromMobile: this.currentAddress.mobile,
          fromProv: this.currentAddress.province,
          fromCity: this.currentAddress.city,
          fromArea: this.currentAddress.area,
          fromAddress: this.currentAddress.address,
          toName: this.currentReceiveAddress.name,
          toMobile: this.currentReceiveAddress.mobile,
          toProv: this.currentReceiveAddress.province,
          toCity: this.currentReceiveAddress.city,
          toArea: this.currentReceiveAddress.area,
          toAddress: this.currentReceiveAddress.address,
          weight: this.weight,
          width: this.volume.width,
          length: this.volume.long,
          height: this.volume.height,
          orderOrigin: orderOrigin[this.selectTab],
          courierOrderExtend: {
            goodsType: this.selectTab === 'send' ? '小件' : '大件',
            jtGoodsType: this.selcetProdType,
            company_bill_code: '',
            company_shop: '线下业务'
          },
          goodsName: this.prodinfo,
          takeGoodsTime: date1,
          takeGoodsTimeEnd: date2,
          msg: this.remark
        }
        //  有type值是微信用户 如果是微信支付
        if(type){
          this.payLoading = true
          this.weChatPay(param)
        // 否则是普通下单 
        }else{
          this.orderLoading = true
          this.createOrder(param)
        }
      }else{
        this.getCourierFreightChargeList(date1,date2,type)
      }
    },
    //下单
    createOrder(param){
      this.$http.post('courierOrder/create', param, res => {
        if(res.code === 'success'){
          this.$uti.alert('系统下单中，请稍后查看')
          uni.switchTab({
            url: `/pages/order/list`
          })
        }else{
          this.$uti.alert(res.message)
        }
        this.orderLoading = false
      })
    },
    // 批量下单
    wechatBatchCreate(date1,date2){
      this.$http.post('courierOrder/wechatBatchCreate', {resources : this.setReceiveList(date1,date2)}, res => {
        if(res.code === 'success'){
          this.$uti.alert('系统下单中，请稍后查看')
        }else{
          this.$uti.alert(res.message)
        }
        this.orderLoading = false
      })
    },
    setReceiveList(date1,date2){
      return this.receiveList.map( item => {
        let ele = {
          id: item.id,
          fromName: this.currentAddress.name,
          fromMobile: this.currentAddress.mobile,
          fromProv: this.currentAddress.province,
          fromCity: this.currentAddress.city,
          fromArea: this.currentAddress.area,
          fromAddress: this.currentAddress.address,
          selectedFrom:[this.currentAddress.province,this.currentAddress.city,this.currentAddress.area],
          selectAddressFrom: null,
          inputTextFrom: `${this.currentAddress.province}${this.currentAddress.city}${this.currentAddress.area}${this.currentAddress.address} ${this.currentAddress.name} ${this.currentAddress.mobile}`,
          toName: item.name,
          toMobile: item.mobile,
          toProv: item.province,
          toCity: item.city,
          toArea: item.area,
          toAddress: item.address,
          weight: Number(this.weight),
          width: Number(this.volume.width),
          length: Number(this.volume.long),
          height: Number(this.volume.height),
          goodsName: item.goodsName,
          orderOrigin: Number(item.weight) > 20 ? '4' : '1',
          courierOrderExtend: {
            goodsType: Number(item.weight) > 20 ? '大件' : '小件',
            jtGoodsType: this.selcetProdType,
            company_bill_code: '',
            company_shop: '线下业务'
          },
          takeGoodsTime: date1,
          takeGoodsTimeEnd: date2, 
          msg: this.remark
        }
        return ele
      });
    },
    // 批量下单接口
    getCourierFreightChargeList(date1,date2,type){
      // 有type值是微信用户
      if(type){
        this.payLoading = true
        this.weChatBatchPay({resources : this.setReceiveList(date1,date2),date1,date2})
      }else{
        this.orderLoading = true
        this.wechatBatchCreate(date1,date2)
      }
    },
    // 批量微信支付
    weChatBatchPay(param){
      let _this = this;
      this.tradeNo = ''
      this.$http.post('courierOrder/getBatchWechatCourierFreightCharge',param, res => {
        console.log(res);
        // tradeNo
        if(res.code === 'success'){
          let params = {
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.packageX,
            signType: res.data.signType,
            paySign: res.data.paySign
          }
          _this.tradeNo = res.data.tradeNo
          // 微信支付
          uni.requestPayment({
            provider: 'wxpay',
            orderInfo: res.data.tradeNo,
            ...params,
            success: function (res) {
              console.log('success:' + JSON.stringify(res));
              if(res.errMsg === 'requestPayment:ok'){
                _this.finishWechatOrder({status:1,tradeNo:_this.tradeNo,openId:getLocal('user').openId})
              }else{
                this.payLoading = false
              }
            },
            fail:(fail)=>{
              this.payLoading = false
              _this.finishWechatOrder({status:2,tradeNo:_this.tradeNo,openId:getLocal('user').openId})
            },
          })
        }else{
          _this.$uti.alert(res.message);
        }
      })
    },
    // wxpay支付
    weChatPay(param){
      let _this = this;
      this.tradeNo = ''
      this.$http.post('courierOrder/getWechatCourierFreightCharge',param, res => {
        console.log(res);
        // tradeNo
        if(res.code === 'success'){
          let params = {
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.packageX,
            signType: res.data.signType,
            paySign: res.data.paySign
          }
          _this.tradeNo = res.data.tradeNo
          // 微信支付
          uni.requestPayment({
            provider: 'wxpay',
            orderInfo: res.data.tradeNo,
            ...params,
            success: function (res) {
              console.log('success:' + JSON.stringify(res));
              if(res.errMsg === 'requestPayment:ok'){
                _this.finishWechatOrder({status:1,tradeNo:_this.tradeNo,openId:getLocal('user').openId})
              }else{
                this.payLoading = false
              }
              
            },
            fail:(fail)=>{
              this.payLoading = false
              _this.finishWechatOrder({status:2,tradeNo:_this.tradeNo,openId:getLocal('user').openId})
            },
          })
        }else{
          _this.$uti.alert(res.message);
        }
      })

    },
    //更新微信订单状态
    finishWechatOrder(param){
      //status 1-支付成功，2-支付失败
      //tradeNo 支付单号
      //openId 微信openId
      this.$http.get('wechat/finishWechatOrder',param,res=>{
        this.payLoading = false
        if(res.code === 'success'){
          this.$uti.alert('系统下单中，请稍后查看')
        }else{
          this.$uti.alert(res.message)
        }
      })
    },
    //获取下单费用接口
    getCourierFreightCharge(){
      const orderOrigin = {
        'big': '4',
        'send': '1'
      }
      let param = {
        time: [],
        fromName: this.currentAddress.name,
        fromMobile: this.currentAddress.mobile,
        fromProv: this.currentAddress.province,
        fromCity: this.currentAddress.city,
        fromArea: this.currentAddress.area,
        fromAddress: this.currentAddress.address,
        toName: this.currentReceiveAddress.name,
        toMobile: this.currentReceiveAddress.mobile,
        toProv: this.currentReceiveAddress.province,
        toCity: this.currentReceiveAddress.city,
        toArea: this.currentReceiveAddress.area,
        toAddress: this.currentReceiveAddress.address,
        weight: this.weight,
        width: this.volume.width,
        length: this.volume.long,
        height: this.volume.height,
        orderOrigin: orderOrigin[this.selectTab],
        courierOrderExtend: {
          goodsType: this.selectTab === 'send' ? '小件' : '大件',
          jtGoodsType: this.selcetProdType,
          company_bill_code: '',
          company_shop: '线下业务'
        }
      }
      this.$http.post('courierOrder/getCourierFreightCharge', param, res => {
        if(res.code === 'success'){
          this.totalPrice = res.data.totalPrice
          this.expressTips = res.data.info
        }else{
          this.$uti.alert(res.message);
        }
			})
    },
    /**
     * 处理选项卡点击事件
     * 当用户点击某个选项卡时，此方法将被调用，以更新当前选项卡的索引
     * 
     * @param {Object} item - 被点击的选项卡对象，包含index属性
     * @returns {void} - 此方法没有返回值
     */
    tabClick(item) {
      this.selectTab = item.type
      console.log(this.selectTab);
      this.current = item.index;
    }
  },
};
</script>

<style lang="scss" scoped>
  .send-express {
    .send-express-card{
      background: #fff;
      box-sizing: border-box;
      margin: 24rpx;
      border-radius: 16rpx;
      .send-express-card-send{
        width: 100%;
        display: flex;
        align-items: center;
        padding: 24rpx;
        box-sizing: border-box;
        justify-content: space-between;
        .send-icon{
          width: 48rpx;
          height: 48rpx;
          margin-right: 16rpx;
        }
        .send-info-content{
          width: 452rpx;
          flex-shrink: 0;
        }
        .send-info{
          display: flex;
          align-items: center;
          .send-info-title{
            color: #333;
            font-size: 32rpx;
            font-weight: bold;
          }
          .send-info-tips{
            color: #999;
            font-size: 24rpx;
            display: flex;
            align-items: center;
            text{
              margin: 8rpx;
            }
          }
        }
      }
      .send-express-card-round--trip{
        display: flex;
        align-items: center;
        padding: 0 24rpx;
        .round--trip-icon{
          width: 36rpx;
          height: 36rpx;
          margin-right: 16rpx;
          transform: rotate(90deg);
        }
        .round--trip-border{
          width: 100%;
          border: 2rpx solid #999;
        }
      }
    }
    .receive-list{
      box-sizing: border-box;
      margin: 24rpx;
      .receive-list-card{
        margin-bottom: 24rpx;
        border-radius: 16rpx;
      }
      .receive-prod-tips{
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 36rpx;
        color: #666;
        padding: 24rpx;
        background-color: #fff;
        border-top: 2rpx solid #999;
        border-bottom-right-radius: 24rpx;
        border-bottom-left-radius: 24rpx;
        .prod-tips{
          display: flex;
          align-items: center;
          .tips-text{
            margin-right: 16rpx;
          }
          .text-bold{
            color: #333;
          }
        }
      }
    }
    .send-express-card-receive{
      width: 100%;
      display: flex;
      align-items: center;
      padding: 24rpx;
      box-sizing: border-box;
      justify-content: space-between;
      background: #fff;
      border-bottom-left-radius: 16rpx;
      border-bottom-right-radius: 16rpx;
      .receive-icon{
        width: 48rpx;
        height: 48rpx;
        margin-right: 16rpx;
      }
      .receive-info-content{
        width: 452rpx;
        flex-shrink: 0;
      }
      .receive-info{
        display: flex;
        align-items: center;
        .receive-info-title{
          color: #333;
          font-size: 32rpx;
          font-weight: bold;
        }
        .receive-info-tips{
          color: #999;
          font-size: 24rpx;
          display: flex;
          align-items: center;
          text{
            margin: 8rpx;
          }
        }
      }
    }
    .address-book{
      font-size: 24rpx;
      color: #333;
      padding-left: 24rpx;
      border-left: 2rpx solid #999;
    }
    .add-receive-arr{
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 36rpx;
      color: #666;
      margin: 0 24rpx 24rpx 24rpx;
      padding: 24rpx;
      background-color: #fff;
      border-radius: 16rpx;
      .plus-receive-btn{
        display: flex;
        align-items: center;
        justify-content: center;
        width: 300rpx;
        height: 48rpx;
        font-size: 28rpx;
        cursor: pointer;
      }
    }
    .send-express-prod{
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 36rpx;
      color: #666;
      margin: 0 24rpx 24rpx 24rpx;
      padding: 24rpx;
      background-color: #fff;
      border-radius: 16rpx;
      .prod-title{
        width: 160rpx;
        flex-shrink: 0;
      }
      .prod-tips{
        display: flex;
        align-items: center;
        .tips-text{
          margin-right: 16rpx;
        }
        .text-bold{
          color: #333;
        }
      }
    }
    .send-express-pick-up{
      margin: 0 24rpx;
      background-color: #fff;
      border-radius: 16rpx;
      padding: 24rpx;
      margin-bottom: 140rpx;
      .pick-up-title{
        font-size: 32rpx;
        color: #666;
        margin-bottom: 16rpx;
      }
      .pick-up-card{
        width: 240rpx;
        height: 180rpx;
        border: 2rpx solid #ed1c24;
        border-radius: 16rpx;
        font-size: 48rpx;
        font-weight: bold;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 16rpx;
        color: #ed1c24;
      }
      .pick-up-time{
        display: flex;
        align-items: center;
        justify-content: space-between;
        color: #999;
        .pick-up-time-select{
          color: #333;
          font-weight: bold;
          display: flex;
          align-items: center;
          .select-show-time{
            margin-right: 16rpx;
          }
        }
      }
    }
    .send-express-pay{
      width: 100%;
      padding: 24rpx;
      box-sizing: border-box;
      position: fixed;
      bottom: 0;
      left: 0;
      background-color: #fff;
      .send-express-pay-tips{
        font-size: 24rpx;
        color: #ed1c24;
        margin-bottom: 8rpx;
        word-break: break-all;
      }
      .send-express-pay-btns{
        display: flex;
        align-items: center;
        justify-content: space-between;
        .pay-detail{
          display: flex;
          align-items: center;
          .pay-detail-text{
            font-size: 36rpx;
            text:first-child{
              color: #333;
            }
            text:last-child{
              color: #e69b3f;
              font-weight: bold;
            }
          }
        }
        .pay-btns{
          display: flex;
          align-items: center;
          .pay-btns-one{
            width: 230rpx;
            color: #fff;
            font-weight: bold;
            font-size: 20rpx;
            background-color: #e69b3f;
            text-align: center;
            padding: 12rpx 24rpx;
            display: flex;
            flex-direction: column;
            border-radius: 40rpx;
            margin-right: 16rpx;
          }
          button{
            width: 98rpx;
            height: 88rpx;
            border-radius: 40rpx;
          }
        }
      }
    }
    .prod-info-box{
      box-sizing: border-box;
      background-color: #f5f5f5;
      .prod-info-box--title{
        font-weight: bold;
        font-size: 36rpx;
        color: #333;
        background-color: #fff;
        padding: 24rpx;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        box-sizing: border-box;
        .prod-banned{
          color: #666;
          font-size: 24rpx;
          display: flex;
          align-items: center;
          margin-right: 60rpx;
        }
      }
      .prod-info-box-main{
        margin-top: 16rpx;
        max-height: 1000rpx;
        overflow-y: scroll;
        .main-card{
          margin-bottom: 16rpx;
          background-color: #fff;
          padding: 0 24rpx;
          .main-card-title{
            font-weight: bold;
            font-size: 32rpx;
            color: #333;
            padding: 24rpx 0;
            display: flex;
            align-items: center;
            text:last-child{
              background: #fff7e09c;
              color: #ed1c24;
              font-size: 24rpx;
              padding: 4rpx;
              display: flex;
              align-items: center;
              justify-content: center;
              margin-left: 8rpx;
            }
            .card-title-volume{
              width: 90rpx;
              flex-shrink: 0;
            }
          }
          .main-card-input{
            padding-bottom: 24rpx;
            display: flex;
            align-items: center;
            .card-input{
              height: 88rpx;
            }
          }
          .main-card-input-text{
            font-size: 24rpx;
            padding-bottom: 24rpx;
            color: #999;
            text{
              color: #ed1c24;
            }
          }
          .prod-card-box{
            display: flex;
            flex-wrap: wrap;
            .prod-card{
              width: 160rpx;
              margin: 16rpx;
              border: 2rpx solid #999;
              padding: 8rpx 16rpx;
              text-align: center;
              border-radius: 32rpx;
              font-size: 24rpx;
            }
            .prod-card-active{
              background: #ed1c24;
              color: #fff;
              border-color: #ed1c24;
            }
          }
          .volume-input{
            font-size: 26rpx;
          }
        }
        .card-weight{
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 24rpx;
        }
      }
      .prod-info-box-btn{
        background-color: #fff;
        padding: 24rpx;
        .info-confirm{
          color:#ffffff;
          background-color:#e69b3f;
          border-color:#e69b3f;
          width:100%;
          font-size: 36rpx;
          border-radius: 40rpx;
        }
      }
    }
    .arrival-time-box{
      box-sizing: border-box;
      background-color: #fff;
      .arrival-time-box--title{
        font-weight: bold;
        font-size: 36rpx;
        color: #333;
        background-color: #fff;
        padding: 24rpx;
      }
      .arrival-date-time{
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        margin-bottom: 16rpx;
        padding: 0 24rpx;
        .arrival-btn{
          padding: 12rpx 16rpx;
          font-size: 24rpx;
          color: #333;
          display: flex;
          align-items: center;
          justify-content: center;
          border: 2rpx solid #999;
          border-radius: 8rpx;
          cursor: pointer;
          margin: 0 16rpx 16rpx 0;
        }
        .arrival-btn-active{
          background: #ed1c24;
          color: #fff;
          border: 2rpx solid #ed1c24;
        }
      }
      .arrival-time-box-btn{
        background-color: #fff;
        padding: 24rpx;
        .time-confirm{
          color:#ffffff;
          background-color:#e69b3f;
          border-color:#e69b3f;
          width:100%;
          font-size: 36rpx;
          border-radius: 40rpx;
        }
      }
    }
  }
</style>
